package com.hamizhong.iflowers.db.dao.hibernate.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.hamizhong.iflowers.db.dao.base.MyBaseHibernateDao;
import com.hamizhong.iflowers.db.dao.hibernate.IFlowerFloridDao;
import com.hamizhong.iflowers.db.entity.FlowerFlorid;

public class FlowerFloridDaoImpl extends MyBaseHibernateDao<FlowerFlorid, Integer> implements IFlowerFloridDao {

	public List<FlowerFlorid> findFlowerFlorid(final Integer pageNum, final int pageSize) {
		List<FlowerFlorid> list = null;
		try {
			List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					//sortId 值大的排前面
					String hql = "from FlowerFlorid t ";
					hql += " order by t.sortId desc";
					Query query = session.createQuery(hql);
					query.setFirstResult((pageNum-1)*pageSize);
					query.setMaxResults(pageSize);
					return query.list();
				}
			});
			list = result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	public int findTotalFlowerFloridCnt() {
		try {
			List totalCntList = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					String hql = "select count(*) from FlowerFlorid";
					Query query = session.createQuery(hql);
					return query.list();
				}
			});
			return Integer.valueOf(totalCntList.get(0).toString()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

}
